package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqWaterProductionEquipmentInspectionMapper;
import com.guofen.pojo.SjqWaterProductionEquipmentInspection;
import com.guofen.service.sjq.SjqWaterProductionEquipmentInspectionService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqWaterProductionEquipmentInspectionServiceImpl implements SjqWaterProductionEquipmentInspectionService{

    @Autowired
    private SjqWaterProductionEquipmentInspectionMapper sjqWaterProductionEquipmentInspectionMapper;


    @Override
    public boolean addForm(String refluxPump, String sludgePump, String pneumaticValve1, String pneumaticValve2,
                           String membraneModule1, String membraneModule2, String membraneLevel, String remarks) {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();

        SjqWaterProductionEquipmentInspection sjqWaterProductionEquipmentInspection = new SjqWaterProductionEquipmentInspection();
        sjqWaterProductionEquipmentInspection.setDate(date);
        sjqWaterProductionEquipmentInspection.setRefluxPump(refluxPump);
        sjqWaterProductionEquipmentInspection.setSludgePump(sludgePump);
        sjqWaterProductionEquipmentInspection.setPneumaticValve1(pneumaticValve1);
        sjqWaterProductionEquipmentInspection.setPneumaticValve2(pneumaticValve2);
        sjqWaterProductionEquipmentInspection.setMembraneModule1(membraneModule1);
        sjqWaterProductionEquipmentInspection.setMembraneModule2(membraneModule2);
        sjqWaterProductionEquipmentInspection.setMembraneLevel(membraneLevel);
        sjqWaterProductionEquipmentInspection.setRemarks(remarks);

        int result = sjqWaterProductionEquipmentInspectionMapper.insert(sjqWaterProductionEquipmentInspection);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public SjqWaterProductionEquipmentInspection findForm() {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();
        System.out.println("当前日期" + date);
        //获取最新记录
        SjqWaterProductionEquipmentInspection sjqWaterProductionEquipmentInspection = sjqWaterProductionEquipmentInspectionMapper.findLast();
        System.out.println("最新记录" + sjqWaterProductionEquipmentInspectionMapper.findLast());
        //获取最新记录日期
        String dateLast =  sjqWaterProductionEquipmentInspection.getDate();
        System.out.println("最新记录日期" + dateLast);

        if (date.equals(dateLast))
            return sjqWaterProductionEquipmentInspection;
        else
            return null;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqWaterProductionEquipmentInspection> list = sjqWaterProductionEquipmentInspectionMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqWaterProductionEquipmentInspection> list = sjqWaterProductionEquipmentInspectionMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqWaterProductionEquipmentInspectionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqWaterProductionEquipmentInspection record) {
        return sjqWaterProductionEquipmentInspectionMapper.updateByPrimaryKeySelective(record);
    }
}
